home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / multi_jitter / multi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  1.5 KB  |  70 lines

  1. #include <math.h>
  2.  
  3.  
  4. #define RAN_DOUBLE(l, h)    (((double) random()/0x80000000U)*((h) - (l)) + (l))
  5. #define RAN_INT(l, h)        ((int) (RAN_DOUBLE(0, (h)-(l)+1) + (l)))
  6.  
  7.  
  8. typedef struct {
  9.     double x, y;
  10. } Point2;
  11.  
  12.  
  13. unsigned long random();        /* expected to return a random int in [0, 2^31-1] */
  14.  
  15.  
  16. /*
  17.  * MultiJitter() takes an array of Point2's and the dimension, and fills the
  18.  * the array with the generated sample points.
  19.  *
  20.  *    p[] must have length m*n.
  21.  *    m is the number of columns of cells.
  22.  *    n is the number of rows of cells.
  23.  */
  24. void
  25. MultiJitter(Point2 p[], int m, int n) {
  26.  
  27.     double subcell_width;
  28.     int i, j;
  29.  
  30.     subcell_width = 1.0/(m*n);
  31.  
  32.     /* Initialize points to the "canonical" multi-jittered pattern. */
  33.     for (i = 0; i < m; i++) {
  34.     for (j = 0; j < n; j++) {
  35.         p[i*n + j].x = i*n*subcell_width + j*subcell_width
  36.          + RAN_DOUBLE(0, subcell_width);
  37.         p[i*n + j].y = j*m*subcell_width + i*subcell_width
  38.          + RAN_DOUBLE(0, subcell_width);
  39.     }
  40.     }
  41.  
  42.     /* Shuffle x coordinates within each column of cells. */
  43.     for (i = 0; i < m; i++) {
  44.     for (j = 0; j < n; j++) {
  45.  
  46.         double t;
  47.         int k;
  48.  
  49.         k = RAN_INT(j, n - 1);
  50.         t = p[i*n + j].x;
  51.         p[i*n + j].x = p[i*n + k].x;
  52.         p[i*n + k].x = t;
  53.     }
  54.     }
  55.  
  56.     /* Shuffle y coordinates within each row of cells. */
  57.     for (i = 0; i < n; i++) {
  58.     for (j = 0; j < m; j++) {
  59.  
  60.         double t;
  61.         int k;
  62.  
  63.         k = RAN_INT(j, m - 1);
  64.         t = p[j*n + i].y;
  65.         p[j*n + i].y = p[k*n + i].y;
  66.         p[k*n + i].y = t;
  67.     }
  68.     }
  69. }
  70.